package com.info.biz;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.info.data.BaseProvider;
import com.info.data.GlobalData;
import com.info.entidy.AttachmentDefine;

public class AttachmentBiz {
	private BaseProvider provider=new BaseProvider();
	public boolean saveAttachment(AttachmentDefine define){
		List<AttachmentDefine> list=new ArrayList<AttachmentDefine>();
		list.add(define);
		if(define!=null){
			return provider.insertAttachmentDefine(list);
		}
		return false;
	}
	
	public List<AttachmentDefine> getAttachmentByFileName(String name){
		List<AttachmentDefine>  list= provider.getAttachmentDefine(name);
		return list ;
	}
	
	public boolean deleteAttachment(List<AttachmentDefine> list){
		boolean isOk=false;
		if(list!=null && list.size()>0){
			isOk=provider.deleteAttachmentDefine(list);
			if(isOk){
				try{
					List<String> filePath=new ArrayList<String>();
					for (AttachmentDefine define : list) {
						filePath.add(define.getTag());
					}
					File file=null;
					for (String path : filePath) {
						file=new File(GlobalData.upFilePath+File.separator+path);
						
						System.out.println("*file name:"+file.getName());
						boolean  isDelete=false;
						if(file.exists())
							isDelete=file.delete();
						System.out.println(" is delete:"+isDelete);
					} 
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			
			
		
		}{
			isOk =true;
		}
		return isOk;
	}

}
